package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.mapper.RessBookMapper;
import com.sky.result.Result;
import com.sky.service.RessBookService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.BagUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class RessBookServiceImpl implements RessBookService{
    @Autowired
    RessBookMapper ressBookMapper;
    /*
    * 新增地址
    * */
    @Override
    public void orderinter(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        ressBookMapper.addBook(addressBook);
    }
/*
* 查询地址
* */
    @Override
    public List<AddressBook> list(AddressBook addressBook) {
       List<AddressBook> list = ressBookMapper.list(addressBook);
        return list;
    }
/*
* 根据id查询
* */
    @Override
    public AddressBook liseBid(long id) {
        AddressBook addressBook = ressBookMapper.listBid(id);
        return addressBook;
    }
/*
* 跟据id修改
* */
    @Override
    public void update(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
       List<AddressBook> list =  ressBookMapper.list(addressBook);
        if (list !=null && list.size()>0){
            ressBookMapper.update(addressBook);
        }
    }
/*
* 根据id删除地址
* */
    @Override
    public void deteleBId(long id) {
        ressBookMapper.deleteByid(id);
    }
/*
* 修改默认地址
* */
    @Override
    @Transactional
    public void updatedeflt(AddressBook addressBook) {
        //现根据uderid修改所有默认地址为o
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        ressBookMapper.updateByUid(addressBook);

        //然后在根据id修改默认地址为1
        addressBook.setIsDefault(1);
        ressBookMapper.update(addressBook);
    }

}
